package se.sics.tac.aw;

import java.util.Vector;

public class InsertionSort {	
	
	/* 
	 * Insertion sort - should be more efficient that quicksort for arrays less than 20
	 * Sorts in ascending order.
	 */

	public static int[] insertionSort(int[] list, int length) {
	    int indx, location, temp;
	    
	    
	    for(indx = 1; indx < length; indx++) { 
	        if(list[indx] < list[indx - 1]) { 
	            temp = list[indx];
	            location = indx;
	            
	            do { 
	                list[location] = list[location-1];
	                location--;
	            }
	            while (location > 0 && list[location-1] > temp);
	            
	            list[location] = temp;
	        }
	    }
	    return list;
	}

	public static Vector<Float> sortAscending(Vector<Float> list) {
	    for(int index = 1; index < list.size(); index++) { 
	        if(list.get(index) < list.get(index - 1)) { 
	            Float temp = list.get(index);
	            int location = index;
	            
	            do { 
	                list.set(location, list.get(location-1));
	                location--;
	            }
	            while (location > 0 && list.get(location-1) > temp);
	            
	            list.set(location, temp);
	        }
	    }
	    return list;
	}
	
	public static Vector<Float> sortDescending(Vector<Float> list) {
	    for(int index = 1; index < list.size(); index++) { 
	        if(list.get(index) > list.get(index - 1)) { 
	            Float temp = list.get(index);
	            int location = index;
	            
	            do { 
	                list.set(location, list.get(location-1));
	                location--;
	            }
	            while (location > 0 && list.get(location-1) < temp);
	            
	            list.set(location, temp);
	        }
	    }
	    return list;
	}
}
